<?php

namespace plugin\admin\app\common\service;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use support\Redis;

class thApi {
    
    const URL = 'https://api-gw.onebound.cn/jd/item_get/';

    /**
     * @throws GuzzleException
     * @throws Exception
     */
    public static function getData($goodsId, $adminInfo): array
    {
        // 1. 采集
        $formData = [
            'key'     => admin('key'),
            'secret'  => admin('token'),
            'num_iid' => $goodsId
        ];
        $sendRequest = new sendRequest();
        $response = $sendRequest->sendRequest('GET', self::URL, [], $formData);
        if($response['status_code'] == 200) {
            switch ($response['body']['error_code']) {
                case 0000:
                    // 处理数据
                    $returnData = self::operationData($goodsId, $response['body']);
                    break;
                case 2000:
                    $returnData = ['status' => false, 'data' => [], 'msg' => "未搜索到相关数据", 'code' => 1, 'statusCode' => 1011];
                    break;
                case 4013:
                    $returnData = ['status' => false, 'data' => [], 'msg' => "无上品权限", 'code' => 0, 'statusCode' => 1012];
                    break;
                case 4016:
                    $returnData = ['status' => false, 'data' => [], 'msg' => "余额不足，请充值" , 'code' => 0, 'statusCode' => 1012];
                    break;
                case 4017:
                    $returnData = ['status' => false, 'data' => [], 'msg' => "请求超时" , 'code' => 0, 'statusCode' => 1012];
                    break;
                case 5000:
                    $returnData = ['status' => false, 'data' => [], 'msg' => "未知错误1" , 'code' => 0, 'statusCode' => 1012];
                    break;
                default:
                    $returnData = ['status' => false, 'data' => [], 'msg' => "未知错误2" , 'code' => 0, 'statusCode' => 1012];
                    break;
            }
            return $returnData;
        } else {
            return ['status' => false, 'data' => [], 'msg' => "未知错误3，请联系管理员" , 'code' => 0, 'statusCode' => 1012];
        }
    }

    /**
     * 返回的数据进行操作
     */
    public static function operationData($goodsId, $res): array
    {
        $res = json_decode(json_encode($res), true);
        // 1. 判断详情是否存在，如果不存在直接返回
        if(count($res['item']['desc_img']) == 0) {
            foreach ($res['item']['item_imgs'] as $k => $v){
                $res['item']['desc_img'][$k] = "https://" . substr($v['url'], 2);
            }
            // 删除第一张主图
            array_shift($res['item']['desc_img']);
        } else {
            foreach ($res['item']['desc_img'] as $k => $v){
                $res['item']['desc_img'][$k] = "https://" . substr($v, 2);
            }
        }
        if(is_array($res['item']['skus']['sku']) && count($res['item']['skus']['sku']) > 0) {
            foreach ($res['item']['skus']['sku'] as $key => $value) {
                if($value['sku_id'] == $goodsId) {
                    $res['item']['price'] = $value['price'];
                }
            }
        }
        $multiSkus[0]['sku_id'] = $goodsId;
        $data = [
            "type" => "spider",
            'code' => 0000,
            'item' => [
                "num_iid"   => $goodsId,
                "title"     => $res['item']['title'],
                'price'     => $res['item']['price'],
                'desc'      => [],
                'desc_img'  => $res['item']['desc_img'],
                "item_imgs" => $res['item']['item_imgs'],
                'skus' => [
                    'sku' =>  $multiSkus
                ],
                'sku_list' => $res['item']['skus']['sku'],
                'props_list' => $res['item']['props_list'],
                'props_img' => $res['item']['props_img'],
                'props' => [['name' => 'props', 'value' => 'props'], ['name' => 'props_', 'value' => 'props_']],
            ]
        ];
        return ['status' => true, 'data' => $data, 'code' => 1, 'statusCode' => 1010];
    }

}
